package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DoubleUtil;
import com.ruoyi.system.domain.SysExpense;
import com.ruoyi.system.domain.SysExpenseStat;
import com.ruoyi.system.domain.SysExpenseStatTotal;
import com.ruoyi.system.service.ISysExpenseMergeService;
import com.ruoyi.system.service.ISysExpenseService;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/system/expenseStat")
public class SysExpenseStatController {

    private String prefix = "system/expenseStat";

    @Autowired
    private ISysExpenseService sysExpenseService;

    @Autowired
    private ISysExpenseMergeService sysExpenseMergeService;

    @RequiresPermissions("system:expenseStat:view")
    @GetMapping()
    public String expense()
    {
        return prefix + "/expenseStat";
    }

    /**
     *   收租统计页面数据
     * @param startTime,endTime
    **/
    @PostMapping("/statData")
    @ResponseBody
    public AjaxResult statData(String startTime,String endTime){
        List<SysExpenseStat> list = sysExpenseMergeService.selectSysExpenseStat(startTime,endTime);
        List<String> buildingName = new ArrayList<>();
        List<String> monthFee = new ArrayList<>();
        List<String> realMonthFee = new ArrayList<>();
        double totalMonthFee = 0L,totalRealMonthFee = 0L;
        for (SysExpenseStat sysExpenseStat : list) {
            buildingName.add(sysExpenseStat.getBuildingName());
            monthFee.add(String.valueOf(DoubleUtil.round(Double.parseDouble(sysExpenseStat.getTotalMonthFee()),2)));
            realMonthFee.add(String.valueOf(DoubleUtil.round(Double.parseDouble(sysExpenseStat.getTotalMonthRealFee()),2)));
            totalMonthFee+=Double.parseDouble(sysExpenseStat.getTotalMonthFee());
            totalRealMonthFee+=Double.parseDouble(sysExpenseStat.getTotalMonthRealFee());
        }
        List<String> feeTypeName = new ArrayList<>();
        feeTypeName.add("应收费用累计");
        feeTypeName.add("实收费用累计");

        List<SysExpenseStatTotal> totaFeelList = new ArrayList<>();
        SysExpenseStatTotal sysExpenseStat = new SysExpenseStatTotal();
        SysExpenseStatTotal sysExpenseRealStat = new SysExpenseStatTotal();
        sysExpenseStat.setName("应收费用累计");
        sysExpenseStat.setValue(String.valueOf(DoubleUtil.round(totalMonthFee,2)));
        sysExpenseRealStat.setName("实收费用累计");
        sysExpenseRealStat.setValue(String.valueOf(DoubleUtil.round(totalRealMonthFee,2)));
        totaFeelList.add(sysExpenseStat);
        totaFeelList.add(sysExpenseRealStat);

        Map resultMap = new HashMap();
        resultMap.put("barBuildingName",buildingName);
        resultMap.put("barMonthFee",monthFee);
        resultMap.put("barRealMonthFee",realMonthFee);
        resultMap.put("pieFeeTypeName",feeTypeName);
        resultMap.put("pieTotaFeelList",totaFeelList);

        return AjaxResult.success(resultMap);
    }
}
